package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import android.view.View;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.response.GetCodeResponse;
import sz.szsmk.citizencard.model.network.dzbean.req.CheckPwdRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.MoneyRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.BankCard;
import sz.szsmk.citizencard.model.network.dzbean.res.CheckPsdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetMyBankResponse;
import sz.szsmk.citizencard.mvp.contract.IFirstRechargeContact;
import sz.szsmk.citizencard.mvp.presenter.IBasePresenter;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/11 16:44
 * description:
 */
public class FirstRechargePresenter extends IBasePresenter
        implements IFirstRechargeContact.IFirstRechargePresenter<IFirstRechargeContact.IFirstRechargeView> {

    private NetworkDateSource mNetworkDateSource;
    GsdxNetApi gsdxNetApi;
    private ArrayList<BankCard> list = new ArrayList<>();
    private final String token;
    private final String channelId;
    private final String openId;
//    private ArrayList<BankCard> list = new ArrayList<>();

    @Inject
    public FirstRechargePresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSource.getInstance();
        token = ACache.get(DaggerApplication.getIns()).getAsString("token");
        channelId = ACache.get(DaggerApplication.getIns()).getAsString("channelId");
        openId = ACache.get(DaggerApplication.getIns()).getAsString("openId");


    }


    @Override
    public void chargeMoney(int money, String bankCard) {

        MoneyRequest moneyRequest = new MoneyRequest();
        moneyRequest.setMoney(money);
        moneyRequest.setBankCardId(bankCard);
        ObjectSignatureUtil.signatureRequest(moneyRequest);
        mNetworkDateSource.recharge(moneyRequest, new Action1<GetCodeResponse>() {
            @Override
            public void call(GetCodeResponse getCodeResponse) {
                String code = getCodeResponse.getCode();
                if (code.equals("0000")) {
                    ((IFirstRechargeContact.IFirstRechargeView) mView).chargeMoneySucc();
                } else {
                    ((IFirstRechargeContact.IFirstRechargeView) mView).chargeMoneyFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    @Override
    public void checkPwdSetting(String openId, String channelId, String token, final View view) {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setOpenId(openId);
        ObjectSignatureUtil.signatureZhsz(commonRequest, token);
        String json = new Gson().toJson(commonRequest);
        L.e(Constant.TAG, "checkPwdSetting---->" + json);
        mNetworkDateSource.checkPwdSetting(commonRequest, new Action1<CheckPsdRes>() {
            @Override
            public void call(CheckPsdRes ibaseBean) {

                if (ibaseBean.getCode().equals("0000")) {
                    ((IFirstRechargeContact.IFirstRechargeView) mView).checkPwdSettingSucc(ibaseBean, view);
                } else {

                }
            }

        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void checkPayPassword(String password, String openId, String token, String channelId) {
        CheckPwdRequest checkPwdRequest = new CheckPwdRequest();
        checkPwdRequest.setPassword(password);
        checkPwdRequest.setOpenId(openId);

        ObjectSignatureUtil.signatureZhsz(checkPwdRequest, token);
        mNetworkDateSource.checkPayPassword(checkPwdRequest, new Action1<GetMyBankResponse>() {
            @Override
            public void call(GetMyBankResponse getMyBankResponse) {
                String code = getMyBankResponse.getCode();
                if (code.equals("0000")) {
                    ((IFirstRechargeContact.IFirstRechargeView) mView).checkPayPasswordSucc();
//                    getMvpView().onCheckSuccess();
                } else {
//                    getMvpView().onCheckFailed();
                    PubUtils.popTipOrWarn(DaggerApplication.getIns(), getMyBankResponse.getMessage());
                    ((IFirstRechargeContact.IFirstRechargeView) mView).checkPayPasswordFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void gainMyBankCard() {

//        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setOpenId(openId);
        ObjectSignatureUtil.signatureZhsz(commonRequest, token);

        mNetworkDateSource.getMyBanks(commonRequest, new Action1<GetMyBankResponse>() {
            @Override
            public void call(GetMyBankResponse getMyBankResponse) {
                if (getMyBankResponse.getCode().equals("0000")) {
                    List<GetMyBankResponse.DataBean> data = getMyBankResponse.getData();
                    if (data == null) {
                        return;
                    }
                    for (int i = 0; i < data.size(); i++) {
                        GetMyBankResponse.DataBean dataBean = data.get(i);
                        String json = new Gson().toJson(dataBean);
                        BankCard bankCard = new Gson().fromJson(json, BankCard.class);
                        list.add(bankCard);
                    }
                    ((IFirstRechargeContact.IFirstRechargeView) mView).gainMyBankCardSucc(list);
//                    getMvpView().onGainMyBankCard(list);
                } else {
//                    getMvpView().onGainMyBankCardFail();
                }

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
